﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace HF.Repository
{
    public static class Enumerable
    {
        public static T[] ToArray<T>(this IEnumerable<T> source, int index, int pageSize)
        {
            if (index < 1)
                index = 1;
            return source.Skip((index - 1)*pageSize).Take(pageSize).ToArray();
        }

        public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (T enumerable in source)
            {
                action(enumerable);
            }
        }

        public static bool TruelyEquals(this object obj1, object obj2)
        {
            if (obj1 == obj2)
            {
                return true;
            }
            if (null == obj2 || null == obj1)
            {
                return false;
            }

            if (obj1.GetType() == obj2.GetType())
            {
                if (obj1 is string)
                {
                    return ((string) obj1).Equals((string) obj2);
                }

                if (obj1 is int)
                {
                    return ((int) obj1).Equals((int) obj2);
                }
                if (obj1 is DateTime)
                {
                    return ((DateTime) obj1).Equals((DateTime) obj2);
                }
                if (obj1 is bool)
                {
                    return ((bool) obj1).Equals((bool) obj2);
                }
                if (obj1 is byte)
                {
                    return ((byte) obj1).Equals((byte) obj2);
                }
                if (obj1 is short)
                {
                    return ((short) obj1).Equals((short) obj2);
                }
                if (obj1 is float)
                {
                    return ((float) obj1).Equals((float) obj2);
                }
                if (obj1 is double)
                {
                    return ((double) obj1).Equals((double) obj2);
                }
                if (obj1 is long)
                {
                    return ((long) obj1).Equals((long) obj2);
                }
                if (obj1 is decimal)
                {
                    return ((decimal) obj1).Equals((decimal) obj2);
                }
            }
            return false;
        }
    }
}